import { Scene } from "../../../../../Core/Module/Entity/Scene";
import { EventSystem } from "../../../../../Core/Module/EventSystem/EventSystem";
import { Log } from "../../../../../Core/Module/Log/Log";
import { Wait_SceneChangeFinish } from "../../../../Model/Client/Demo/Scene/Wait_SceneChangeFinish";
import { SessionComponent } from "../../../../Model/Client/Demo/Session/SessionComponent";
import { PlayerComponent } from "../../../../Model/Client/Demo/Unit/PlayerComponent";
import { C2G_EnterMap, G2C_EnterMap } from "../../../../Model/Generate/Share/Message/OuterMessage_C_10001";
import { EventType } from "../../../../Model/Share/Demo/EventType";
import { ObjectWait } from "../../../../Model/Share/Module/ObjectWait/ObjectWait";

export class EnterMapHelper {

    public static async EnterMapAsync(clientScene: Scene) {
        try {
            const g2CEnterMap = await clientScene.GetComponent<SessionComponent>(SessionComponent).Session.Call(new C2G_EnterMap()) as G2C_EnterMap;
            clientScene.GetComponent<PlayerComponent>(PlayerComponent).MyId = g2CEnterMap.MyId;
            // 等待场景切换完成
            await clientScene.GetComponent<ObjectWait>(ObjectWait).Wait<Wait_SceneChangeFinish>(Wait_SceneChangeFinish);
            EventSystem.Instance.Publish(clientScene, new EventType.EnterMapFinish());
        } catch (e) {
            Log.Error(e);
        }
    }

}